﻿using ICSharpCode.TextEditor.Document;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace controls.TextEditor
{
    internal class FoldingTime : IFoldingStrategy
    {
        public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
        {
            //每次重新折叠；
            flag = true;

            List<FoldMarker> list = new List<FoldMarker>();
            //stack
            var startLines = new Stack<int>();
            var startLines1 = new Stack<int>();

            //Regex r = new Regex(@"^(^(\d{4}|\d{2})(\-|\/|\.)\d{1,2}\3\d{1,2}$)|(^\d{4}年\d{1,2}月\d{1,2}日$)");
            Regex r = new Regex(@"^(^(\d{4}|\d{2})(\-|\/|\.)\d{1,2}\3\d{1,2})|(^\d{4}年\d{1,2}月\d{1,2}日)");
            bool islast = false;
            int last = 0;
            // Create foldmarkers for the whole document, enumerate through every line.
            for (int i = 0; i < document.TotalNumberOfLines; i++)
            {
                // Get the text of current line.
                string text = document.GetText(document.GetLineSegment(i));

                //if (text.Trim().StartsWith("#region")) // Look for method starts
                //{
                //    startLines1.Push(i);

                //}

                //if (text.Trim().StartsWith("#endregion")) // Look for method endings
                //{
                //    islast = true;
                //    last = i;
                //    int start = startLines1.Pop();
                //    // Add a new FoldMarker to the list.
                //    // document = the current document
                //    // start = the start line for the FoldMarker
                //    // document.GetLineSegment(start).Length = the ending of the current line = the start column of our foldmarker.
                //    // i = The current line = end line of the FoldMarker.
                //    // 7 = The end column
                //    list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.Region, "...", true));
                //}

                Match m = r.Match(text.Trim());
                //var b = m.Success;
                if (m.Success)
                {
                    if (flag == false)
                    {
                        if (startLines.Count > 0)
                        {
                            int start = startLines.Pop();
                            // 判断相邻两行是否都有标签
                            if (start == i - 1)
                            {
                                startLines.Push(start);
                            }
                            else
                            {
                                if (islast)
                                {
                                    list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, last - 1, 57, FoldType.TypeBody, "...}", true));
                                    islast = false;
                                }
                                else
                                {
                                    list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i - 1, 57, FoldType.TypeBody, "...}", true));
                                }
                                //if (count < 150)
                                //{
                                //    list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i - 1, 57, FoldType.TypeBody, "...}", true));
                                //}
                                //else
                                //{
                                //    list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i - 1, 57, FoldType.TypeBody, "...}", false));
                                //}
                                //count++;
                                startLines.Push(i);
                            }
                        }
                    }
                    //第一次加载标签
                    else
                    {
                        flag = false;
                        startLines.Push(i);
                    }
                }
            }

            //int start1 = startLines.Pop();
            //list.Add(new FoldMarker(document, start1, document.GetLineSegment(start1).Length, last - 1, 57, FoldType.TypeBody, "...}", true));
            //count = 0;
            return list;
        }

        private int count = 0;
        private bool flag = true;
    }
}